<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Adv_Model extends CI_Model
{
    protected $_lang = '';
	function __construct() {
        parent::__construct();
    }
    public function initialise($lang)
    {
        if($lang!=''&&$lang!='vn')
            $this->_lang = '_'.$lang;
        else
            $this->_lang = '';
    }
    function loadAdvPositionByPage($page){
        $this->db->select('adv_page.adv_page_name,adv_page.adv_page_code,adv_position.*');
        
        $this->db->join('adv_page','adv_position.adv_page_id = adv_page.adv_page_id');
        
        $this->db->where('adv_page.adv_page_code',$page);
        $this->db->where('adv_position.bl_active',1);
        $query = $this->db->get('adv_position');
        return $query->result();
    }
    function loadAdvByPosition($adv_position_id){
        $this->db->where('adv.adv_position_id',$adv_position_id);
        $this->db->where('adv.bl_active',1);
        $query = $this->db->get('adv');
        return $query->result();
    }
    function getAdvById($adv_id){
        $this->db->where('bl_active',1);
        $this->db->where('adv_id',$adv_id);
         return $this->db->get('adv')->row();
    }
    function saveAdv($adv_id,$data){
        if($adv_id!=0){
            $this->db->where('adv_id',$adv_id);
            if($this->db->update('adv',$data)){
                 return true;
            }else{
                 return false;
            }
        }else{
            if($this->db->insert('adv',$data)){
                return true;

            }else{
                return false;
            }
       }
    }
    function writeAdvLog($data){
        $this->db->insert('adv_log',$data);
    }
    function secondsClick($adv_id,$ip){
        $sql = "SELECT COUNT(*) as total,UNIX_TIMESTAMP(NOW())-dt_log as secondsclick from (SELECT *
                FROM aw_adv_log
                WHERE adv_id=".$adv_id."
                    AND adv_log_ip = '".$ip."'
                    AND adv_log_type = 1
                ORDER BY dt_log DESC LIMIT 1) as max_log";
        $query = $this->db->query($sql);
        return $query->row();
    }
}